var prefix = jsctx + "/marketActivity";

var marketActivityRecord = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageMarketActivityRecord",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    cache: false,
                    pageSize: 20,
                    pageList: [20],
                    pageNumber: 1,
                    sidePagination: "server",
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            beginTime: $("#beginTime").val().trim(),
                            endTime: $("#endTime").val().trim(),
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '', width: '50', align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },

                        {field: 'activityName', title: '活动名称', width: '100', align: 'left'},
                        {
                            field: 'rewardType', title: '奖励类型',
                            width: '100', align: 'center',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span>赠送优惠券</span>'
                                } else if (value == '1') {
                                    return '<span>消费返利</span>'
                                }
                            }
                        },
                        {
                            field: 'sendType', title: '赠送对象',
                            width: '100', align: 'center',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span >新用户</span>'
                                } else if (value == '1') {
                                    return '<span>邀请者</span>'
                                }
                            }
                        },
                        {
                            field: '', title: '奖品',
                            width: '100', align: 'center',
                            formatter: function (value, row, index) {
                                if (row.discountType == '0') {
                                    return '<span>' + '满' + row.useRequireAmount + '立减' + row.discountAmount + '元' + '</span>'
                                } else if (row.discountType == '1') {
                                    return '<span>' + '满' + row.useRequireAmount + '享' + (row.discount * 10) + '折' + '</span>'
                                }
                            }
                        },
                        {field: 'sendCount', title: '单次赠送数量', width: '100', align: 'left'},
                        {field: 'ticketName', title: '关联优惠券', width: '100', align: 'left'},
                        {field: 'sendMemberCount', title: '成功参与人数', width: '100', align: 'left'},
                        {field: 'sendTicketCount', title: '发放总量', width: '100', align: 'left'},
                        {field: 'beginTime', title: '活动开始时间', width: '100', align: 'left'},
                        {field: 'endTime', title: '活动结束时间', width: '100', align: 'left'},
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function goBack() {
        window.history.back();
    }


    function addMarketActivity() {
        $.ajax({
            url: prefix + "/isExecuteMarketActivity",
            type: "get",
            success: function (data) {
                if (data.status == 200) {
                    location.href = prefix + "/addMarketActivityNoReturn";
                } else {
                    layer.msg(data.message);
                }
            }
        });
    }


    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        addMarketActivity: addMarketActivity
    };
})();


var addMarketActivity = (function () {
    function init() {
        validateRule();
    };

    function getCheckedRoles() {
        var adIds = "";
        $("input:checkbox[name=role]:checked").each(function (i) {
            if (0 == i) {
                adIds = $(this).val();
            } else {
                adIds += ("," + $(this).val());
            }
        });
        return adIds;
    }

    var isajaxsend = true;

    function save() {
        if (isajaxsend) {
            var formData = new FormData($("#frmMarketActivity")[0]);
            $("#roleIds").val(getCheckedRoles());
            $.ajax({
                cache: true,
                type: "POST",
                url: prefix + "/saveOrUpdateMarketActivity",
                data: formData,
                processData: false,
                contentType: false,
                async: false,
                beforeSend: function () {
                    isajaxsend = false
                },
                error: function (request) {
                    isajaxsend = true
                    parent.layer.alert("Connection error");
                },
                success: function (data) {
                    if (data.status == 200) {
                        isajaxsend = true
                        parent.layer.msg("操作成功");
                        location.href = prefix + "/index";
                    } else {
                        isajaxsend = true
                        parent.layer.alert(data.message)
                    }

                }
            });
        }
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmMarketActivity").validate({
            rules: {
                activityName: {
                    required: true,
                    maxlength: 20
                },
                ticketTypeId: {
                    required: true
                },
                sendCount: {
                    required: true,
                    min: 1,
                    maxlength: 9,
                    digits: true,
                },
                maxDiscountCount: {
                    maxlength: 9
                },
                remark: {
                    maxlength: 500,
                    required: true
                },
                registerPrompt: {
                    maxlength: 100,
                    required: true
                },
                sendPrompt: {
                    maxlength: 30,
                    required: true
                }

            },
            messages: {
                activityName: {
                    required: icon + "请输入活动名称",
                    maxlength: icon + "活动名称字数限制为20"
                },
                ticketTypeId: {
                    required: icon + "请选择优惠券",
                }
                ,
                sendCount: {
                    required: icon + "请输入单次赠送数量",
                    min: icon + "单次赠送数量大于0",
                    maxlength: icon + "最大长度不超过9位",
                    digits: icon + "请输入正整数",
                },
                maxDiscountCount: {
                    maxlength: icon + "最大长度不超过9位"
                },
                remark: {
                    maxlength: icon + "活动规则描述字数限制为500",
                    required: icon + "请输入活动规则描述",
                },
                registerPrompt: {
                    maxlength: icon + "注册成功提示字数限制为200",
                    required: icon + "请输入注册成功提示",
                },
                sendPrompt: {
                    maxlength: icon + "送券成功提示字数限制为30",
                    required: icon + "请输入送券成功提示",
                }
            },
            submitHandler: function (form) {
                save();
            }
        })
    }

    return {
        init: init
    };
})();
